﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aioms.Common;
using Aioms.Dtos;
using Aioms.Models;
using Furion.DynamicApiController;
using Furion.LinqBuilder;
using Furion.UnifyResult;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Aioms.Controllers
{
    /// <summary>
    /// 巡检点
    /// </summary>
    [DynamicApiController]
    public class InspectionOptionController
    {
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public InspectionOptionController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
        }

        /// <summary>
        /// 巡检项列表
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="content">搜索内容</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostList([FromForm] long eid, [FromForm] long ipid, [FromForm] string content, [FromForm] int pageIndex = 1, [FromForm] int pageSize = 10)
        {
            RefAsync<int> total = 0;
            List<InspectionOption> inspectionOptions = await db.Queryable<InspectionOption>().Where(io => io.Eid == eid && io.Ipid == ipid).WhereIF(!string.IsNullOrEmpty(content), io => io.Procedure.Contains(content)).ToPageListAsync(pageIndex, pageSize, total);
            UnifyContext.Fill(new { Total = total.Value});
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = inspectionOptions,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 获取工单冗余巡检点巡检项
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="woid"></param>
        /// <param name="ipid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostRyList([FromForm] long eid, [FromForm] long woid, [FromForm] long ipid, [FromForm] int pageIndex = 1, [FromForm] int pageSize = 10)
        {
            TpWorkOrder workOrders = await db.Queryable<TpWorkOrder>()
                .Where(wo => wo.Eid == eid && wo.Id == woid)
                .SingleAsync();
            XunJianPlan xunjianPlan = workOrders.TaskInfo.ToObject<XunJianPlan>();
            var data = xunjianPlan.XunJianDians.Where(xjd => xjd.Id == ipid).Select(xjd => xjd.XunJianXiangs).FirstOrDefault().Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            UnifyContext.Fill(new { Total = data?.Count });
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = data,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 删除巡检点
        /// </summary>
        /// <param name="eid">企业ID</param>
        /// <param name="ids">ID数组</param>
        /// <returns></returns>
        public async Task<IActionResult> PostDelete([FromForm] long eid, [FromForm] long[] ids) {
            if (ids.Length > 0) {
                await db.Deleteable<InspectionOption>().Where(ip => ip.Eid == eid && ids.Contains(ip.Id)).ExecuteCommandAsync();
            }
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = "删除巡检点成功",
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="inspectionoption"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostSave([FromForm] long eid, [FromForm] InspectionOption inspectionoption)
        {
            InspectionOption last_inspectionoption = await db.Insertable<InspectionOption>(inspectionoption).ExecuteReturnEntityAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = last_inspectionoption,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostShow([FromForm] long eid, [FromForm] long id)
        {
            InspectionOption inspectionoption = await db.Queryable<InspectionOption>().Where(io => io.Eid == eid && io.Id == id).SingleAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = inspectionoption,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="inspectionoption"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostUpdate([FromForm] long eid, [FromForm] InspectionOption inspectionoption)
        {
            await db.Updateable<InspectionOption>(inspectionoption).ExecuteCommandAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

    }
}
